package com.zack.gank.gankandroid.main.find;

import com.gank.zack.ganklibrary.network.retrofit.RetrofitHelper;
import com.orhanobut.logger.Logger;
import com.zack.gank.gankandroid.MyApplication;
import com.zack.gank.gankandroid.api.GankInterface;
import com.zack.gank.gankandroid.main.find.bean.ImageGank;
import com.zack.gank.gankandroid.main.find.bean.TodayGank;
import com.zack.gank.gankandroid.utils.InterfaceUtils;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.internal.schedulers.ScheduledAction;
import rx.schedulers.Schedulers;

/**
 * Created by Android on 2017/7/14.
 */
public class FindSelectPresenter implements FindSelectView.Presenter {

    FindSelectView findView;
    public FindSelectPresenter(FindSelectView findView) {
        this.findView=findView;
    }

    @Override
    public void setGankDate(String date) {
        GankInterface gankInterface= RetrofitHelper.createApi(GankInterface.class);
        Logger.v(date);
        // 没用使用rxjava
//        getSelectData(gankInterface,date);
//        getImageData(gankInterface);
        rxJavaGetData(gankInterface,date);
    }


    private void rxJavaGetData(final GankInterface gankInterface, final String date) {

        Observable<ImageGank> observableToday = gankInterface.getGankImgeData(InterfaceUtils.typeseaxyData, "20", "1");
        observableToday.subscribeOn(Schedulers.newThread())
                .observeOn(Schedulers.io())
                .flatMap(new Func1<ImageGank, Observable<TodayGank>>() {
                    @Override
                    public Observable<TodayGank> call(ImageGank imageGank) {
                        // 获取图片成功 在调用 下一个接口，
                        MyApplication.getContext().setComonImageUrl(imageGank.results);
                        return gankInterface.getGankDataOfDates(date);
                    }
                   }).doOnNext(new Action1<TodayGank>() {
                    @Override
                    public void call(TodayGank todayGank) {
                        // 可以存数据
                    }
                }).subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<TodayGank>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(TodayGank todayGank) {
                        // 返回
                        findView.onSucess(todayGank);
                    }
                });
    }
/*
    private void getSelectData(GankInterface gankInterface,String date){


        Call<TodayGank> objectCall=gankInterface.getGankDataOfDate(date);


         objectCall.enqueue(new Callback<TodayGank>() {
            @Override
            public void onResponse(Call<TodayGank> call, Response<TodayGank> response) {
//                Logger.v(response.body().toString());
                findView.onSucess(response.body());
            }

            @Override
            public void onFailure(Call<TodayGank> call, Throwable t) {
                findView.onFile(t);
            }
        });
    }*/

    /**
     * 获取默认图片 在没有图片的情况下
     * @param gankInterface
     */
    private void getImageData(GankInterface gankInterface){
     /*   Call<ImageGank> objectCall=gankInterface.getGankImgeData(InterfaceUtils.typeseaxyData,"20","1");
        objectCall.enqueue(new Callback<ImageGank>() {
            @Override
            public void onResponse(Call<ImageGank> call, Response<ImageGank> response) {
                Logger.v(response.body().toString());
                findView.onImageSucess(response.body());
            }

            @Override
            public void onFailure(Call<ImageGank> call, Throwable t) {
                findView.onFile( t);
            }
        });*/
    }
}
